package com.complaintcase.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.complaintcase.common.core.domain.entity.SysUser;
import com.complaintcase.common.core.domain.model.LoginUser;
import com.complaintcase.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.complaintcase.common.annotation.Log;
import com.complaintcase.common.core.controller.BaseController;
import com.complaintcase.common.core.domain.AjaxResult;
import com.complaintcase.common.enums.BusinessType;
import com.complaintcase.system.domain.CaseInfo;
import com.complaintcase.system.service.ICaseInfoService;
import com.complaintcase.common.utils.poi.ExcelUtil;
import com.complaintcase.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 投诉案件信息Controller
 * 
 * @author ruoyi
 * @date 2025-09-20
 */
@RestController
@RequestMapping("/system/info")
public class CaseInfoController extends BaseController
{
    @Autowired
    private ICaseInfoService caseInfoService;

    /**
     * 查询投诉案件信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(CaseInfo caseInfo)
    {
        startPage();
        List<CaseInfo> list = caseInfoService.selectCaseInfoList(caseInfo);
        return getDataTable(list);
    }

    /**
     * 导出投诉案件信息列表
     */
//    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "投诉案件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CaseInfo caseInfo)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser user = loginUser.getUser();

        List<CaseInfo> list = caseInfoService.selectCaseInfoList(caseInfo);
        ExcelUtil<CaseInfo> util = new ExcelUtil<CaseInfo>(CaseInfo.class);
        if (user.getDept()!=null && !user.getDept().getExportAll()){
            util.showColumn("id","month","caseNo","recordingDate","recordingTime","relationDept","relationTeam","employeeId","employeeName","serviceProblem","serviceAttitude","serviceSkills","businessSolutions","processExecution","answerDescription","issuesSuggestions","isGoodRecording","attitudeCategory","solvingCategory","otherCategory","replyStaffId","replyDate","replyTime","advantageDescription");
        }

        util.exportExcel(response, list, "投诉案件信息数据");

    }

    /**
     * 获取投诉案件信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(caseInfoService.selectCaseInfoById(id));
    }

    /**
     * 新增投诉案件信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "投诉案件信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CaseInfo caseInfo)
    {
        return toAjax(caseInfoService.insertCaseInfo(caseInfo));
    }

    /**
     * 修改投诉案件信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "投诉案件信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CaseInfo caseInfo)
    {
        return toAjax(caseInfoService.updateCaseInfo(caseInfo));
    }

    /**
     * 删除投诉案件信息
     */
//    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "投诉案件信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(caseInfoService.deleteCaseInfoByIds(ids));
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CaseInfo> util = new ExcelUtil<CaseInfo>(CaseInfo.class);
        util.importTemplateExcel(response, "导出模板");
    }


    @Log(title = "导入", businessType = BusinessType.IMPORT)
//    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CaseInfo> util = new ExcelUtil<CaseInfo>(CaseInfo.class);
        List<CaseInfo> userList = util.importExcel(file.getInputStream(),1);
        Long operName = getUserId();
        String message = caseInfoService.importCaseInfo(userList, operName);
        return success(message);
    }

}
